gusucode.com > 各种VC自绘控件源码程序 > 各种VC自绘控件源码/code/SkinControls(自绘MFC基本控件 )/SkinControls/SkinControls/SkinDialog.cpp

    
#include "stdafx.h"
#include "AfxPriv.h"
#include "SkinDialog.h"
#include "MemDC.h"

//////////////////////////////////////////////////////////////////////////

//按钮标识
#define IDC_CLOSE					50001
#define IDC_MAX						50002
#define IDC_MIN						50003
#define IDC_RES						50004

IMPLEMENT_DYNAMIC(CSkinDialog, CDialog)

BEGIN_MESSAGE_MAP(CSkinDialog, CDialog)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_CTLCOLOR()
	ON_WM_ACTIVATE()
	ON_WM_ACTIVATEAPP()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_RBUTTONDOWN()
	ON_WM_RBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_ERASEBKGND()
	ON_WM_SETTINGCHANGE()
	ON_WM_WINDOWPOSCHANGED()
	ON_MESSAGE(WM_SETTEXT,OnSetTextMesage)
	ON_BN_CLICKED(IDC_CLOSE, OnBnClickedClose)
	ON_BN_CLICKED(IDC_MIN, OnBnClickedMin)
	ON_BN_CLICKED(IDC_MAX, OnBnClickedMax)
	ON_BN_CLICKED(IDC_RES, OnBnClickedRes)
END_MESSAGE_MAP()

IMPLEMENT_DYNAMIC(CSkinRgnDialog, CDialog)

BEGIN_MESSAGE_MAP(CSkinRgnDialog,CDialog)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()


//////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////

//构造函数
CSkinDialog::CSkinDialog(UINT nIDTemplate, CWnd * pParentWnd) 
	: CDialog(nIDTemplate,pParentWnd)
{
	m_bActive=false;
	m_bMaxSize=false;
	m_bOnlyClose=true;
	m_bExitMax = false;
	m_bAdjustClose = false;
	m_bAdjustMaxRes = false;
	m_bAdjustMin = false;
}

//析构函数
CSkinDialog::~CSkinDialog()
{
}

//初始化
void CSkinDialog::InitSkinObject()
{
	//设置变量
	m_bActive=false;

	m_BtClose.Create(TEXT(""), WS_CHILD, CRect(0,0,0,0), this, IDC_CLOSE);
	m_BtMin.Create(TEXT(""), WS_CHILD, CRect(0,0,0,0), this, IDC_MIN);
	m_BtMax.Create(TEXT(""), WS_CHILD, CRect(0,0,0,0), this, IDC_MAX);
	m_btRestore.Create(TEXT(""), WS_CHILD, CRect(0,0,0,0), this, IDC_RES);
	m_BtClose.SetToolTips(TEXT("关闭"));
	m_BtMin.SetToolTips(TEXT("最小化"));
	m_BtMax.SetToolTips(TEXT("最大化"));
	m_btRestore.SetToolTips(TEXT("还原"));

	//计算属性
	ModifyStyle(WS_CAPTION,0,SWP_DRAWFRAME);
	ModifyStyleEx(WS_EX_CLIENTEDGE|WS_EX_WINDOWEDGE,0,0);
	GetWindowRect(&m_crNormalSize);
	LONG lWindowStyte=GetWindowLongPtr(m_hWnd,GWL_STYLE);
	if (lWindowStyte&WS_SYSMENU)
	{
		m_BtClose.ShowWindow(SW_SHOW);
		if ((lWindowStyte&WS_MAXIMIZEBOX)||(lWindowStyte&WS_MINIMIZEBOX)) 
		{
			m_bOnlyClose = false;
			if (lWindowStyte&WS_MAXIMIZEBOX) 
			{
				m_BtMax.ShowWindow(SW_SHOW);
				m_bExitMax = true;
			}
			if (lWindowStyte&WS_MINIMIZEBOX) m_BtMin.ShowWindow(SW_SHOW);
		}
	}


	////强制修改Border样式
	//if (lWindowStyte & WS_DLGFRAME || lWindowStyte & WS_SIZEBOX)
	//{
	//	SetWindowLong(m_hWnd, GWL_STYLE, WS_BORDER);
	//	SetWindowLong(m_hWnd, GWL_EXSTYLE,0); 
	//	SetWindowPos(NULL,m_crNormalSize.left,m_crNormalSize.top,m_crNormalSize.Width(),m_crNormalSize.Height(),SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
	//}

	AdjustTitleButton();
	
	return;
}

//调整按钮区域
void CSkinDialog::AdjustTitleButton()
{
	CRect ClientRect;
	GetClientRect(&ClientRect);

	CRect rcClose;
	m_BtClose.GetWindowRect(&rcClose);
	if (m_bAdjustClose)
		m_BtClose.MoveWindow(m_ptClose.x, m_ptClose.y, rcClose.Width(), rcClose.Height());
	else
		m_BtClose.MoveWindow(ClientRect.right-10-rcClose.Width(), ClientRect.top, rcClose.Width(), rcClose.Height());
	CRect rcMax;
	m_BtMax.GetWindowRect(&rcMax);
	if (m_bAdjustMaxRes)
	{
		m_BtMax.MoveWindow(m_ptMaxRes.x, m_ptMaxRes.y, rcMax.Width(), rcMax.Height());
		m_btRestore.MoveWindow(m_ptMaxRes.x, m_ptMaxRes.y, rcMax.Width(), rcMax.Height());
	}
	else
	{
		m_BtMax.MoveWindow(ClientRect.right-10-rcClose.Width()-rcMax.Width(), ClientRect.top, rcMax.Width(), rcMax.Height());
		m_btRestore.MoveWindow(ClientRect.right-10-rcClose.Width()-rcMax.Width(), ClientRect.top, rcMax.Width(), rcMax.Height());
	}
	CRect rcMin;
	m_BtMin.GetWindowRect(&rcMin);
	if (m_bAdjustMin)
		m_BtMin.MoveWindow(m_ptMin.x, m_ptMin.y, rcMin.Width(), rcMin.Height());
	else
	{
		int nMinX = (m_bExitMax==true)?(ClientRect.right-10-rcClose.Width()-rcMax.Width()-rcMin.Width()):(ClientRect.right-10-rcClose.Width()-rcMin.Width());
		m_BtMin.MoveWindow(nMinX, ClientRect.top, rcMin.Width(), rcMin.Height());
	}

}

void CSkinDialog::AdjustCloseButton(int x, int y)
{
	m_ptClose.x = x;
	m_ptClose.y = y;
	m_bAdjustClose = true;
	CRect rcClose;
	m_BtClose.GetWindowRect(&rcClose);
	m_BtClose.MoveWindow(x, y, rcClose.Width(), rcClose.Height());
}
void CSkinDialog::AdjustMaxResButton(int x, int y)
{
	m_ptMaxRes.x = x;
	m_ptMaxRes.y = y;
	m_bAdjustMaxRes = true;
	CRect rcClose;
	m_BtMax.GetWindowRect(&rcClose);
	m_BtMax.MoveWindow(x, y, rcClose.Width(), rcClose.Height());
	m_btRestore.MoveWindow(x, y, rcClose.Width(), rcClose.Height());
}
void CSkinDialog::AdjustMinButton(int x, int y)
{
	m_ptMin.x = x;
	m_ptMin.y = y;
	m_bAdjustMin = true;
	CRect rcClose;
	m_BtMin.GetWindowRect(&rcClose);
	m_BtMin.MoveWindow(x, y, rcClose.Width(), rcClose.Height());
}

CSize CSkinDialog::GetCloseButtonSize()
{
	CRect rcClose;
	m_BtClose.GetWindowRect(&rcClose);
	return CSize(rcClose.Width(), rcClose.Height());
}
CSize CSkinDialog::GetMaxResButtonSize()
{
	CRect rcClose;
	m_BtMax.GetWindowRect(&rcClose);
	return CSize(rcClose.Width(), rcClose.Height());
}
CSize CSkinDialog::GetMinButtonSize()
{
	CRect rcClose;
	m_BtMin.GetWindowRect(&rcClose);
	return CSize(rcClose.Width(), rcClose.Height());
}

//初始化消息
BOOL CSkinDialog::OnInitDialog()
{
	__super::OnInitDialog();

	//初始化
	InitSkinObject();
	SetClassLong(m_hWnd,GCL_HBRBACKGROUND,NULL);

	return TRUE;
}

void CSkinDialog::DoDataExchange(CDataExchange* pDX)
{
	__super::DoDataExchange(pDX);

	//DDX_Control(pDX, IDC_CLOSE, m_BtClose);
	//DDX_Control(pDX, IDC_MAX, m_BtMax);
	//DDX_Control(pDX, IDC_MIN, m_BtMin);
}

//上面边框(标题)高度
INT CSkinDialog::GetTopBorderHeight()
{
	return m_BorderTM.GetHeight();
}
//下面边框高度
INT CSkinDialog::GetBottomBorderHeight()
{
	return m_BorderBM.GetHeight();
}

//左边框宽度
INT CSkinDialog::GetLeftBorderWidth()
{
	return m_BorderML.GetWidth();
}

//右边框宽度
INT CSkinDialog::GetRightBorderWidth()
{
	return m_BorderMR.GetWidth();
}


//激活消息
void CSkinDialog::OnActivate(UINT nState, CWnd * pWndOther, BOOL bMinimized)
{
	__super::OnActivate(nState,pWndOther,bMinimized);

	m_bActive=(nState!=WA_INACTIVE);
	Invalidate(FALSE);

	return;
}

//激活消息
void CSkinDialog::OnActivateApp(BOOL bActive, DWORD dwThreadID)
{
	__super::OnActivateApp(bActive,dwThreadID);

	m_bActive=bActive?true:false;
	Invalidate(FALSE);

	return;
}

//创建区域
bool CSkinDialog::SetDlgRgn()
{
	CRect rcRect;
	GetClientRect(&rcRect);
	CRgn rgnWnd;
	rgnWnd.CreateRectRgn(0,0,0,0);


	CRgn rgnTL, rgnTR, rgnBL, rgnBR;
	GetImageRgn(rgnTL, &m_BorderTL);
	GetImageRgn(rgnTR, &m_BorderTR);
	GetImageRgn(rgnBL, &m_BorderBL);
	GetImageRgn(rgnBR, &m_BorderBR);

	CRgn rgnTM;
	rgnTM.CreateRectRgn(m_BorderTL.GetWidth(), 0, rcRect.right - m_BorderTR.GetWidth(), m_BorderTR.GetHeight());
	rgnWnd.CombineRgn(&rgnTL, &rgnTM, RGN_OR);
	rgnTR.OffsetRgn(rcRect.right-m_BorderTR.GetWidth(), 0);
	rgnWnd.CombineRgn(&rgnWnd, &rgnTR,RGN_OR);
	CRgn rgnM;
	rgnM.CreateRectRgn(0, m_BorderTR.GetHeight(), rcRect.Width(), rcRect.bottom-m_BorderBR.GetHeight());
	rgnWnd.CombineRgn(&rgnWnd, &rgnM, RGN_OR);
	rgnBL.OffsetRgn(0, rcRect.bottom-m_BorderBL.GetHeight());
	rgnWnd.CombineRgn(&rgnWnd, &rgnBL, RGN_OR);
	CRgn rgnBM;
	rgnBM.CreateRectRgn(m_BorderBL.GetWidth(), rcRect.bottom-m_BorderBL.GetHeight(), rcRect.right-m_BorderBR.GetWidth(), rcRect.bottom);
	rgnWnd.CombineRgn(&rgnWnd, &rgnBM, RGN_OR);
	rgnBR.OffsetRgn(rcRect.right-m_BorderBR.GetWidth(), rcRect.bottom-m_BorderBR.GetHeight());
	rgnWnd.CombineRgn(&rgnWnd, &rgnBR, RGN_OR);

	SetWindowRgn(rgnWnd,TRUE);

	return true;
}

//鼠标消息
void CSkinDialog::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	__super::OnLButtonDblClk(nFlags,point);

	//最大化窗口
	if ((point.y<GetTopBorderHeight())&&(m_BtMax.IsWindowVisible())&&
		(m_BtMax.IsWindowEnabled()))
	{
		if (m_bMaxSize==true) RestoreWindow();
		else MaxSizeWindow();
	}

	return;
}

//鼠标消息
void CSkinDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
	__super::OnLButtonDown(nFlags,point);

	//模拟按标题
	if (m_bMaxSize==false)
	{
		if (point.y<GetTopBorderHeight())
		{
			PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
			return ;
		}
		return;
	}

	return;
}

//鼠标消息
void CSkinDialog::OnLButtonUp(UINT nFlags, CPoint point)
{
	__super::OnLButtonUp(nFlags,point);

	return;
}

//鼠标消息
void CSkinDialog::OnRButtonDblClk(UINT nFlags, CPoint point)
{
	__super::OnRButtonDblClk(nFlags,point);

	return;
}

//鼠标消息
void CSkinDialog::OnRButtonDown(UINT nFlags, CPoint point)
{
	__super::OnRButtonDown(nFlags,point);

	return;
}

//鼠标消息
void CSkinDialog::OnRButtonUp(UINT nFlags, CPoint point)
{
	__super::OnRButtonUp(nFlags,point);

	return;
}

//鼠标消息
void CSkinDialog::OnMouseMove(UINT nFlags, CPoint point)
{
	__super::OnMouseMove(nFlags,point);

	//设置光标
	SetCursor(LoadCursor(NULL,MAKEINTRESOURCE(IDC_ARROW)));

	return;
}

//控件颜色
HBRUSH CSkinDialog::OnCtlColor(CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
	switch (nCtlColor)
	{
	//case CTLCOLOR_DLG:
	case CTLCOLOR_STATIC:
	case CTLCOLOR_BTN:
		{
			pDC->SetBkMode(TRANSPARENT);
			return   HBRUSH(GetStockObject(HOLLOW_BRUSH));
			//pDC->SetBkColor(m_SkinAttribute.m_crBackGround);
			//return m_SkinAttribute.m_brBackGround;
		}
	}
	return __super::OnCtlColor(pDC,pWnd,nCtlColor);
}

//最大窗口
void CSkinDialog::MaxSizeWindow()
{
	if (m_bMaxSize==false)
	{
		//计算位置
		CRect rcClient;
		GetClientRect(&rcClient);
		ClientToScreen(&rcClient);
		GetWindowRect(&m_crNormalSize);

		//移动窗口
		CRect rcRect;
		SystemParametersInfo(SPI_GETWORKAREA,0,&rcRect,SPIF_SENDCHANGE);
		rcRect.top-=(rcClient.top-m_crNormalSize.top);
		rcRect.bottom+=(m_crNormalSize.bottom-rcClient.bottom);
		rcRect.left-=(rcClient.left-m_crNormalSize.left);
		rcRect.right+=(m_crNormalSize.right-rcClient.right);
		MoveWindow(&rcRect,TRUE);
		SetDlgRgn();
		m_BtMax.ShowWindow(SW_HIDE);
		m_btRestore.ShowWindow(SW_SHOW);
		AdjustTitleButton();
		Invalidate(FALSE);

		//设置变量
		m_bMaxSize=true;
	}

	return;
}

//还原窗口
void CSkinDialog::RestoreWindow()
{
	if (m_bMaxSize==true)
	{
		//移动窗口
		MoveWindow(&m_crNormalSize,TRUE);
		SetDlgRgn();
		m_BtMax.ShowWindow(SW_SHOW);
		m_btRestore.ShowWindow(SW_HIDE);
		AdjustTitleButton();

		//设置变量
		m_bMaxSize=false;
		Invalidate(FALSE);
	}
	return;
}

void CSkinDialog::DrawSkinView(CDC * pDC)
{
	//获取参数
	CRect ClientRect;
	GetClientRect(&ClientRect);

	CMemDC BufferDC(pDC, &ClientRect);

	BufferDC.FillSolidRect(&ClientRect, RGB(255,255,255));

	m_BorderTL.Draw(BufferDC.m_hDC, 0, 0);
	for (DWORD i = m_BorderTL.GetWidth(); i < ClientRect.right-m_BorderTR.GetWidth(); i += m_BorderTM.GetWidth())
	{
		m_BorderTM.Draw(BufferDC.m_hDC, i, 0);
	}
	m_BorderTR.Draw(BufferDC.m_hDC, ClientRect.right-m_BorderTR.GetWidth(), 0);


	for (DWORD nYPos = m_BorderTL.GetHeight(); nYPos < ClientRect.bottom - m_BorderBL.GetHeight(); nYPos += m_BorderML.GetHeight())
	{
		m_BorderML.Draw(BufferDC.m_hDC, 0, nYPos);
		m_BorderMR.Draw(BufferDC.m_hDC, ClientRect.right-m_BorderMR.GetWidth(), nYPos);
	}

	m_BorderBL.Draw(BufferDC.m_hDC, 0, ClientRect.bottom - m_BorderBL.GetHeight());
	for (DWORD i = m_BorderBL.GetWidth(); i < ClientRect.right-m_BorderBR.GetWidth(); i += m_BorderBM.GetWidth())
	{
		m_BorderBM.Draw(BufferDC.m_hDC, i, ClientRect.bottom - m_BorderBM.GetHeight());
	}
	m_BorderBR.Draw(BufferDC.m_hDC, ClientRect.right-m_BorderBR.GetWidth(), ClientRect.bottom - m_BorderBM.GetHeight());

	m_ImageTitle.Draw(BufferDC.m_hDC, 10, (m_BorderTL.GetHeight()-m_ImageTitle.GetHeight())/2);

		//获取标题
	TCHAR strTitle[128]=TEXT("");
	GetWindowText(strTitle,sizeof(strTitle));

	//计算位置
	INT nYPos1=m_BorderTL.GetHeight()/2-6;
	INT nXPos1=10;
	if (m_ImageTitle.IsValid()) nXPos1 += m_ImageTitle.GetWidth()+5;

	BufferDC.SetBkMode(TRANSPARENT);
	//BufferDC.SelectObject(CSkinResourceManager::GetDefaultFont());
	CFont TitleFont;
	TitleFont.CreateFont(-12, 0, 0, 0, FW_BOLD, 0, 0, 0, 134, 3, 2, 1, 2, TEXT("宋体"));
	BufferDC.SelectObject(&TitleFont);
	BufferDC.SetTextAlign(TA_LEFT);

	//BufferDC.SetTextColor(m_SkinAttribute.m_crCaptionTXColor);
	BufferDC.TextOut(nXPos1,nYPos1,strTitle);
}

//绘画消息 
void CSkinDialog::OnPaint()
{
	CPaintDC dc(this);
	
	DrawSkinView(&dc);

	return;
}

//位置改变
void CSkinDialog::OnWindowPosChanged(WINDOWPOS * lpwndpos)
{
	__super::OnWindowPosChanged(lpwndpos);

	//调整位置
	//AdjustTitleButton();

	//重画界面
	//Invalidate(FALSE);

	return;
}

//设置改变
void CSkinDialog::OnSettingChange(UINT uFlags, LPCTSTR lpszSection)
{
	__super::OnSettingChange(uFlags, lpszSection);

	//移动窗口
	if (m_bMaxSize==true)
	{
		//计算位置
		CRect rcClient;
		GetClientRect(&rcClient);
		ClientToScreen(&rcClient);
		GetWindowRect(&m_crNormalSize);

		//移动窗口
		CRect rcRect;
		SystemParametersInfo(SPI_GETWORKAREA,0,&rcRect,SPIF_SENDCHANGE);
		rcRect.top-=(rcClient.top-m_crNormalSize.top);
		rcRect.bottom+=(m_crNormalSize.bottom-rcClient.bottom);
		rcRect.left-=(rcClient.left-m_crNormalSize.left);
		rcRect.right+=(m_crNormalSize.right-rcClient.right);
		MoveWindow(&rcRect,FALSE);
		Invalidate(FALSE);
	}

	return;
}

//绘画背景
BOOL CSkinDialog::OnEraseBkgnd(CDC * pDC)
{
	Invalidate(FALSE);
	UpdateWindow();
	return TRUE;
}


//标题消息
LRESULT	CSkinDialog::OnSetTextMesage(WPARAM wParam, LPARAM lParam)
{
	LRESULT lResult=DefWindowProc(WM_SETTEXT,wParam,lParam);
	Invalidate(FALSE);
	return lResult;
}

//设置背景色
void CSkinDialog::SetBackGroundColor(COLORREF crBackColor)
{
}

//设置背景色
void CSkinDialog::SetTitleTextColor(COLORREF crTX)
{
}

//加载关闭位图
bool CSkinDialog::LoadCloseImage(LPCTSTR pszFileName)
{
	m_BtClose.SetButtonImage(pszFileName);
	AdjustTitleButton();
	return true;
}

//加载最小化位图
bool CSkinDialog::LoadMinImage(LPCTSTR pszFileName)
{
	m_BtMin.SetButtonImage(pszFileName);
	AdjustTitleButton();
	return true;
}

//加载最大化位图
bool CSkinDialog::LoadMaxImage(LPCTSTR pszFileName)
{
	m_BtMax.SetButtonImage(pszFileName);
	AdjustTitleButton();
	return true;
}

//加载还原位图
bool CSkinDialog::LoadResImage(LPCTSTR pszFileName)
{
	m_btRestore.SetButtonImage(pszFileName);
	AdjustTitleButton();
	return true;
}

//设置边框位图
void CSkinDialog::SetBorderImage(LPCTSTR pszTL, LPCTSTR pszTM, LPCTSTR pszTR, LPCTSTR pszML, LPCTSTR pszMR, LPCTSTR pszBL, LPCTSTR pszBM, LPCTSTR pszBR)
{
	m_BorderTL.Load(pszTL);
	m_BorderTM.Load(pszTM);
	m_BorderTR.Load(pszTR);
	m_BorderML.Load(pszML);
	m_BorderMR.Load(pszMR);
	m_BorderBL.Load(pszBL);
	m_BorderBM.Load(pszBM);
	m_BorderBR.Load(pszBR);
	m_BorderTL.SetTransIndex(0);
	m_BorderTL.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderTM.SetTransIndex(0);
	m_BorderTM.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderTR.SetTransIndex(0);
	m_BorderTR.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderML.SetTransIndex(0);
	m_BorderML.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderMR.SetTransIndex(0);
	m_BorderMR.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderBM.SetTransIndex(0);
	m_BorderBM.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderBL.SetTransIndex(0);
	m_BorderBL.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));
	m_BorderBR.SetTransIndex(0);
	m_BorderBR.SetTransColor(CxImage::RGBtoRGBQUAD(TRANS_COLOR));

	SetDlgRgn();

	return;
}


//设置标题图片
void CSkinDialog::SetTitleImage(LPCTSTR pszTitleImage)
{
	m_ImageTitle.Load(pszTitleImage);

	return;
}

//按钮消息
void CSkinDialog::OnBnClickedClose()
{
	PostMessage(WM_CLOSE,0,0);
}
void CSkinDialog::OnBnClickedMin()
{
	ShowWindow(SW_MINIMIZE);
}
void CSkinDialog::OnBnClickedMax()
{
	MaxSizeWindow();
}
void CSkinDialog::OnBnClickedRes()
{
	RestoreWindow();
}

//////////////////////////////////////////////////////////////////////////

//构造函数
CSkinRgnDialog::CSkinRgnDialog(UINT uTemplate, CWnd * pParent) : CDialog(uTemplate,pParent)
{
}

//析构函数
CSkinRgnDialog::~CSkinRgnDialog()
{
}

//初始化函数
BOOL CSkinRgnDialog::OnInitDialog()
{
	__super::OnInitDialog();

	//删除窗口标题和获取属性
	ModifyStyle(WS_CAPTION,0,SWP_DRAWFRAME);
	SetClassLong(m_hWnd,GCL_HBRBACKGROUND,NULL);

	return TRUE;
}

void CSkinRgnDialog::DrawBack(CDC * pDC)
{
	//绘画底图
	CRect ClientRect;
	GetClientRect(&ClientRect);
	if (m_ImageBack.IsValid()) m_ImageBack.Draw(pDC->m_hDC,0,0);
}

//重画函数
void CSkinRgnDialog::OnPaint()
{
	CPaintDC dc(this);

	//绘画底图
	DrawBack(&dc);

	return;
}

//加载位图
bool CSkinRgnDialog::LoadRgnImage(LPCTSTR pszFileName)
{
	m_ImageBack.Load(pszFileName);
	return CreateDlgRgn();
}

//创建区域
bool CSkinRgnDialog::CreateDlgRgn()
{

	//创建区域
	CRgn RgnControl;
	GetImageRgn(RgnControl, &m_ImageBack);

	//设置窗口
	if (RgnControl.GetSafeHandle()!=NULL)
	{
		//移动窗口
		SetWindowRgn(RgnControl,TRUE);
		SetWindowPos(NULL,0,0,m_ImageBack.GetWidth(),m_ImageBack.GetHeight(),SWP_NOMOVE);

		//删除区域
		m_DialogRgn.DeleteObject();
		m_DialogRgn.Attach(RgnControl.Detach());

		return true;
	}

	return false;
}

//鼠标右键按下消息
void CSkinRgnDialog::OnLButtonDown(UINT nFlags, CPoint point)
{
	__super::OnLButtonDown(nFlags, point);

	//模拟按标题
	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
}

//背景函数
BOOL CSkinRgnDialog::OnEraseBkgnd(CDC * pDC)
{
	Invalidate(FALSE);
	UpdateWindow();
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////